<template>
  <PageWrapper>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="customHeader"> 导出表格 </a-button>
      </template>
    </BasicTable>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { getBasicColumns, searchFormSchema } from './orderStatisticsTrainData';
  import { getCarOrderByParams } from '/@/api/orderStaiistics/train/trainOrderStaiisticsAPI';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import { toRaw, ref } from 'vue';
  import dayjs from 'dayjs';

  export default defineComponent({
    components: { BasicTable, PageWrapper },
    setup() {
      const [registerTable, { getDataSource }] = useTable({
        api: getCarOrderByParams,
        title: '火车订单统计',
        columns: getBasicColumns(),
        formConfig: {
          labelWidth: 100,
          actionColOptions: { span: 24 },
          schemas: searchFormSchema,
          fieldMapToTime: [
            ['outTicketDate', ['outTicketDateStart', 'outTicketDateEnd'], 'YYYY-MM-DD'],
            ['fromDate', ['fromDateStart', 'fromDateEnd'], 'YYYY-MM-DD'],
            ['arriveDate', ['arriveDateStart', 'arriveDateEnd'], 'YYYY-MM-DD'],
          ],
        },
        pagination: { pageSize: 10 },
        expandRowByClick: true,
        striped: true,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        tableSetting: { fullScreen: true },
      });
      function customHeader() {
        let header = {};
        let orderArr: object[] = [];

        for (let i = 0; i < getBasicColumns().length; i++) {
          // console.log(getBasicColumns()[i].title);
          let key: string = (getBasicColumns()[i].dataIndex as string) ?? '';
          header[key] = getBasicColumns()[i].title;
        }
        header['id'] = '序号';
        for (let k = 0; k < toRaw(getDataSource()).length; k++) {
          let data = ref({
            id: 1,
            a0: '终端类型',
            a1: '预订时间',
            a2: '预订人',
            a3: '出差申请单号',
            a4: '订单号',
            a5: '12306订单号',
            a6: '员工编号',
            a7: '旅客姓名',
            a8: '联系电话',
            a9: '行程',
            a10: '出发日期',
            a11: '出发时间',
            a12: '到达日期',
            a13: '到达时间',
            a14: '车次',
            a15: '席别',
            a16: '座位号',
            a17: '票价',
            a18: '改签手续费',
            a19: '退票手续费',
            a20: 0,
            a21: '线下出票服务费',
            a22: '凭证打印费',
            a23: '售票处服务费',
            a24: '深白云扣款',
            a25: '应付总计',
            a26: '总金额包含服务费和凭证打印费',
            a27: '订单类别',
            a28: '证件号',
            a29: '客票状态',
            a30: '结算状态',
            a31: '成本中心',
            a32: '联系人',
            a33: 'DBI信息',
            a34: 'OA申请单号',
            a35: '业务经理',
            a36: '原订单号',
            a37: '是否vip',
            a38: '提前预订天数',
            a39: '渠道服务费',
            a40: '预订人部门',
            a41: '操作员',
            a42: '支付方式',
            a43: '个人支付金额',
            a44: '个人退款金额',
            a45: '公司支付金额',
            a46: '公司退款金额',
            a47: '交易流水号',
            a48: '费用承担部门',
            a49: '项目编号',
            a50: '项目名称',
            a51: '差旅事项',
            a52: '超标原因',
            a53: '超标金额',
            a54: '超标描述',
            a55: '费用类型',
            a56: '差旅授权号',
            a57: '审批状态',
            a58: '给审批人留言',
            a59: '审批组1类型',
            a60: '审批组1审批人',
            a61: '审批组2类型',
            a62: '审批组2审批人',
            a63: '审批组3类型',
            a64: '审批组3审批人',
            a65: '审批组4类型',
            a66: '审批组4审批人',
            a67: '员工部门',
            a68: '原票价',
            a69: '退改状态',
            a70: '改签类型',
            a71: '退票时间',
            a72: '退票下单人',
            recordId: '消费记录id',
            // a74: '订单号',
            a75: '乘车人卡号',
            a76: '出发城市',
            a77: '到达城市',
          });
          let record = getDataSource()[k];
          data.value.id = k + 1;
          data.value.a0 = record.orderSource === '移动端' ? 'app' : record.orderSource;
          data.value.a1 = record.bookTime
            ? dayjs(record.createTime).format('YYYY-M-D HH:mm:ss')
            : '-';
          data.value.a2 = record.bookerName;
          data.value.a3 = record.voucherId;
          data.value.a4 = record.orderId;
          data.value.a5 = record.idIn12306;
          data.value.a6 = record.accountNo;
          data.value.a7 = record.empName;
          data.value.a8 = record.trainOrder.passengers[0].telephone;
          data.value.a9 = record.fromStation + '-' + record.arriveStation;
          data.value.a10 = dayjs(record.fromDate).format('YYYY-MM-DD');
          data.value.a11 = record.fromTime;
          data.value.a12 = dayjs(record.arriveDate).format('YYYY-MM-DD');
          data.value.a13 = record.arriveTime;
          data.value.a14 = record.trainCode;
          data.value.a15 = record.seatTypeDes;
          data.value.a16 = record.seatNo;
          data.value.a17 = record.ticketMoney;
          data.value.a18 = record.changeCharge ? record.changeCharge : 0;
          data.value.a19 = record.refundCharge ? record.refundCharge : 0;
          data.value.a20 = record.charge;
          data.value.a21 = '0';
          data.value.a22 = '0';
          data.value.a23 = '0';
          data.value.a24 =
            record.ticketMoney + record.charge + record.refundCharge + record.changeCharge;
          data.value.a25 =
            record.ticketMoney + record.charge + record.refundCharge + record.changeCharge;
          data.value.a26 =
            record.ticketMoney + record.charge + record.refundCharge + record.changeCharge;
          data.value.a27 = record.travelType === '出票单' ? '普通订单' : record.travelType;
          data.value.a28 = record.trainOrder.passengers[0].certNo;
          data.value.a29 = record.travelType === '出票单' ? '正常' : record.travelType;
          data.value.a30 = '-';
          data.value.a31 = '-';
          data.value.a32 = record.trainOrder.linkMans[0].linkManName
            ? record.trainOrder.linkMans[0].linkManName
            : '-';
          data.value.a33 = '-';
          data.value.a34 = record.voucherId ? record.voucherId : '-';
          data.value.a35 = record.busiManage ? record.busiManage : '-';
          data.value.a36 = '-';
          data.value.a37 = '-';
          data.value.a38 = '-';
          data.value.a39 = '0';
          data.value.a40 = record.examineApprove ? record.examineApprove.departmentId : '-';
          data.value.a41 = '';
          data.value.a42 = '月结';
          data.value.a43 = '0';
          data.value.a44 = record.refundAmount ? record.refundAmount : 0;
          // data.value.a45 = record.ticketMoney ? record.ticketMoney + record.charge : 0;
          data.value.a45 =
            record.ticketMoney + record.charge + record.refundCharge + record.changeCharge;
          data.value.a46 = record.refundMoney ? record.refundMoney : 0;
          data.value.a47 = '-';
          data.value.a48 = '-';
          data.value.a49 = '-';
          data.value.a50 = '-';
          data.value.a51 = '-';
          data.value.a52 = '-';
          data.value.a53 = '-';
          data.value.a54 = '-';
          data.value.a55 = '-';
          data.value.a56 = '-';
          data.value.a57 = '-';
          data.value.a58 = '-';
          data.value.a59 = '-';
          data.value.a60 = '-';
          data.value.a61 = '-';
          data.value.a62 = '-';
          data.value.a63 = '-';
          data.value.a64 = '-';
          data.value.a65 = '-';
          data.value.a66 = '-';
          data.value.a67 = record.examineApprove != null ? record.examineApprove.departmentId : '-';
          data.value.a68 = '-';
          // record.travelType === '出票单' || record.travelType === '退票单'
          //   ? 0
          //   : record.ticketMoney
          //   ? record.trainOrder.passengers[0].ticketMoney
          //     ? -(
          //         record.trainOrder.passengers[0].ticketMoney +
          //         record.trainOrder.passengers[0].changeCharge +
          //         record.trainOrder.passengers[0].charge +
          //         record.trainOrder.passengers[0].refundMoney
          //       )
          //     : 0
          //   : 0;
          data.value.a69 = record.trainOrder.passengers[0].quitStatus
            ? record.trainOrder.passengers[0].quitStatus == 1
              ? '已退'
              : record.trainOrder.passengers[0].quitStatus == 0
              ? '未退'
              : '-'
            : '-';
          data.value.a70 = record.trainOrder.passengers[0].changeType
            ? record.trainOrder.passengers[0].changeType == 1
              ? '新票款高于原票款，将原单退掉，重新支付新单票款'
              : record.trainOrder.passengers[0].changeType == 2
              ? '新票款与原票款相等，无需支付或退款'
              : record.trainOrder.passengers[0].changeType == 3
              ? '新票款低于原票款，退还差额部分'
              : '-'
            : '-';
          data.value.a71 = record.refundDate ? record.refundDate : '-';
          data.value.a72 = '-';
          data.value.recordId = record.id;
          // data.value.a74 = record.orderId;
          data.value.a75 = record.trainOrder.passengers[0].certNo;
          data.value.a76 = record.fromStation;
          data.value.a77 = record.arriveStation;
          orderArr.push(data.value);
        }

        jsonToSheetXlsx({
          data: orderArr,
          header,
          filename: '火车订单统计' + dayjs(new Date()).format('YYYY-MM-DD') + '.xlsx',
          json2sheetOpts: {
            // 指定顺序
            header: [
              'id',
              'a0',
              'a1',
              'a2',
              'a3',
              'a4',
              'a5',
              'a6',
              'a7',
              'a8',
              'a9',
              'a10',
              'a11',
              'a12',
              'a13',
              'a14',
              'a15',
              'a16',
              'a17',
              'a18',
              'a19',
              'a20',
              'a21',
              'a22',
              'a23',
              'a24',
              'a25',
              'a26',
              'a27',
              'a28',
              'a29',
              'a30',
              'a31',
              'a32',
              'a33',
              'a34',
              'a35',
              'a36',
              'a37',
              'a38',
              'a39',
              'a40',
              'a41',
              'a42',
              'a43',
              'a44',
              'a45',
              'a46',
              'a47',
              'a48',
              'a49',
              'a50',
              'a51',
              'a52',
              'a53',
              'a54',
              'a55',
              'a56',
              'a57',
              'a58',
              'a59',
              'a60',
              'a61',
              'a62',
              'a63',
              'a64',
              'a65',
              'a66',
              'a67',
              'a68',
              'a69',
              'a70',
              'a71',
              'a72',
              'recordId',
              'a75',
              'a76',
              'a77',
            ],
          },
        });
      }
      return {
        registerTable,
        customHeader,
      };
    },
  });
</script>
